/*添加字典及对应的值*/
function setSettings(key, value) {
	window.localStorage.setItem(key, value);
}

/*获取指定字典的值*/
function getSettings(key) {
	var settingsText = window.localStorage.getItem(key);
	return settingsText;
}

/*获取指定字典的值*/
function removeSettings(key) {
	var settingsText = window.localStorage.removeItem(key);
	return settingsText;
}
/*
   封装ajax请求
 * */
function ajaxSetting(opt) {
	$.ajax({
		url: opt.url,
		type: opt.type == null?'post':opt.type,
		data: opt.data,
        dataType: opt.dataType == null?'json':opt.dataType,
        contentType: opt.contentType == null ? 'application/json' : opt.contentType,
        beforeSend: function (request) {
//          request.setRequestHeader("token", 'UjA5T1IwTklSVTVIVjFWWlQxVXhOVFEwTVRRMk1UUTBPVFF3T2prPSQ5ODJDQTBDNDZERkZGNjAxQTJDRTVGQTg1OUI5MUJFNg=='); //getSettings("token")
            request.setRequestHeader("SYSTOKEN", getSettings("token")); //
        },
		success: function(res) {
			console.log(res)
			if(res.statusCode==200){
				opt.success(res);
			}
			else{
				layer.msg(res.message, {icon: 5,anim: 6});
				if(res.statusCode == 401 ){
					window.location.href="/page/system/login.html";
				}
				if(opt.error){
					opt.error(res);
				}
			}
		},
		error: function(res) {
			console.log(res);
			if(opt.error){
					opt.error(res);
			}
			else{
//				window.location.href="/page/system/login.html";
			}
			
		}
	})
}

/*
 * 封装日期格式化函数
 * fmt 日期格式
 * */
Date.prototype.Format = function (fmt) { 
	var o = {
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
		"H+": this.getHours(), //小时
		"m+": this.getMinutes(), //分
		"s+": this.getSeconds(), //秒
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度
		"S": this.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
	if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}
//获取当前时间的前n天，格式YYYY-MM-DD
function getNowFormatDate(time) {
    var date = new Date(new Date().getTime() - time);
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate ;
}
/*获取url中的参数*/
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象

    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

/*
  * js array_searcy() 函数
  * @param array 必选参数 要查找的数组或对象
  * @param find 必须参数 要查找的内容
  */
  function array_search(array, str) {
      if (typeof array !== 'object') {
          return false;
      } else {
          var found = [];
          for (var i = 0; i < array.length; i++) {
              for (var j in array[i]) {
                  if(j!='userName' && j!= "phone" && j!='nickName'){
                      continue;
                  }
                  var value = array[i][j]+'';//转化为字符串
                  if (value.indexOf(str) >= 0) {
                      found.push(array[i]);
                      break;
                  }
              }
          }
          return found;
      }
  }
  //去左空格;
  function ltrim(s) {
      return s.replace(/(^\s*)/g, "");
  }
  //去右空格;
  function rtrim(s) {
      return s.replace(/(\s*$)/g, "");
  }
  //去左右空格;
  function trim(s) {
      return s.replace(/(^\s*)|(\s*$)/g, "");
  }